<?php


namespace App\Services\WeChat;


use EasyWeChat\Factory;
use EasyWeChat\OfficialAccount\Application;

class WeChatService
{
    /**
     * 小程序接口
     */
    static public function mp()
    {
        return Factory::miniProgram([
            'app_id' => env('WECHAT_MINI_PROGRAM_APP_ID'),
            'secret' => env('WECHAT_MINI_PROGRAM_SECRET'),
        ]);
    }

    /**
     * 基础接口
     *
     * @param  array  $params
     *
     * @return Application
     */
    static public function oa($params = [])
    {
        $config = array_merge([
            'app_id'        => config('wechat.official_account.default.app_id'),
            'secret'        => config('wechat.official_account.default.secret'),
            'response_type' => 'array',
        ], $params);

        return Factory::officialAccount($config);
    }

    /**
     * 敏感字检查
     *
     * @param $content
     *
     * @return bool
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    static public function checkText($content)
    {
        try
        {
            $config = [
                'app_id'        => config('wechat.mini_program.default.app_id'),
                'secret'        => config('wechat.mini_program.default.secret'),

                // 下面为可选项
                // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
                'response_type' => 'array',

                'log' => [
                    'level' => 'debug',
                    'file'  => __DIR__ . '/wechat.log',
                ],
            ];

            $app = Factory::miniProgram($config);

            $result = $app->content_security->checkText($content);

            if ($result['errcode'] != '0')
            {
                throw new \Exception('内容有敏感词汇！');
            }

        }
        catch (\Exception $e)
        {
            return false;
        }

        return true;
    }
}
